#!/usr/bin/env bash
#
# Author:
#   mmtrt [Taqi Raza]
#
# Description:
#   download and prepare notepad-plus-plus for snap
#
# Date: Feb 11 2018
#

dlnpp () {
    ver=$(wget https://api.github.com/repos/notepad-plus-plus/notepad-plus-plus/releases -qO - 2>&1 | grep tag_name | cut -d'"' -f4 | sed s'/v//' | head -n1)
    if [ "${ARCHITECTURE}" = "amd64" ]; then
     wget https://github.com/notepad-plus-plus/notepad-plus-plus/releases/download/v${ver}/npp.${ver}.Installer.x64.exe &> /dev/null
     7z x -aos "npp.$ver.Installer.x64.exe" -x'!change.log' -x'!doLocalConf.xml' -x'!LICENSE' -x'!NppShell_06.dll' -x'!readme.txt' -x'!userDefinedLang-markdown.default.modern.xml' -o"usr/share/notepad-plus-plus"
    elif [ "${ARCHITECTURE}" = "i386" ]; then
     wget https://github.com/notepad-plus-plus/notepad-plus-plus/releases/download/v${ver}/npp.${ver}.Installer.exe &> /dev/null
     7z x -aos "npp.$ver.Installer.exe" -x'!change.log' -x'!doLocalConf.xml' -x'!LICENSE' -x'!NppShell_06.dll' -x'!readme.txt' -x'!userDefinedLang-markdown.default.modern.xml' -o"usr/share/notepad-plus-plus"
    else
     echo "ERROR! notepad-plus-plus only produces software for amd64 and i386. Failing the build here."
     exit 1
    fi
}

dli7z16 () {
    ARCHITECTURE=$(dpkg --print-architecture)
    if [ "${ARCHITECTURE}" = "amd64" ]; then
    debs="https://launchpad.net/ubuntu/+source/p7zip/16.02+dfsg-4/+build/13091326/+files/p7zip_16.02+dfsg-4_amd64.deb https://launchpad.net/ubuntu/+source/p7zip/16.02+dfsg-4/+build/13091326/+files/p7zip-full_16.02+dfsg-4_amd64.deb"
    debsi="p7zip_16.02+dfsg-4_amd64.deb p7zip-full_16.02+dfsg-4_amd64.deb"
    elif [ "${ARCHITECTURE}" = "i386" ]; then
    debs="https://launchpad.net/ubuntu/+source/p7zip/16.02+dfsg-4/+build/13091329/+files/p7zip_16.02+dfsg-4_i386.deb https://launchpad.net/ubuntu/+source/p7zip/16.02+dfsg-4/+build/13091329/+files/p7zip-full_16.02+dfsg-4_i386.deb"
    debsi="p7zip_16.02+dfsg-4_i386.deb p7zip-full_16.02+dfsg-4_i386.deb"
    else
     echo "ERROR! No amd64 or i386 system found. Failing the build here."
     exit 1
    fi

    for dldebs in $debs
    do
    wget $dldebs &> /dev/null
    done

    for pkgdebins in $debsi
    do
    sudo apt install ./$pkgdebins -y &> /dev/null
    rm $pkgdebins
    done
}

mknpp () {
    appdir="usr/share/notepad-plus-plus"
    mkdir ./{wine-runtime,wine-platform,bin} && mkdir -p usr/share/{pixmaps,applications} && mkdir -p "winedata/Application Data/Notepad++" && mkdir -p "usr/share/notepad-plus-plus/plugins/Config"
    mv $appdir/notepad++.exe $appdir/notepad-plus-plus.exe
    cp -R $appdir/'$_14_'/* "winedata/Application Data/Notepad++";cp -R $appdir/'$_15_'/* "usr/share/notepad-plus-plus/plugins";cp -R $appdir/'$_17_'/* "usr/share/notepad-plus-plus/plugins/Config"
    find $appdir/'$PLUGINSDIR' -type f -name '*.xml' -print0 | while read -d $'\0' file; do cp -v "$file" $appdir/localization/ &>/dev/null; done
    rm -R $appdir/'$_14_';rm -R $appdir/'$_15_';rm -R $appdir/'$_17_';rm -R $appdir/'$PLUGINSDIR';
    find "usr" -type d -execdir chmod 755 {} + && find "winedata" -type d -execdir chmod 755 "{}" +
}

mkdsk () {
cat > notepad-plus-plus.desktop <<'EOF1'
[Desktop Entry]
Name=Notepad++
GenericName=Text Editor
Comment=Simple and free source code editor.
Encoding=UTF-8
Version=
Type=Application
Terminal=false
Icon=${SNAP}/usr/share/pixmaps/notepad-plus-plus.png
Exec=notepad-plus-plus %F
StartupWMClass=notepad-plus-plus.exe
Categories=Utility;TextEditor
MimeType=text/plain;text/css;text/html;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-java;text/x-makefile;text/x-python;text/x-sh;application/x-wine-extension-ini;application/x-wine-extension-inf;
EOF1
sed -i -e 's|Version=|Version='"$ver"'|g' notepad-plus-plus.desktop
}

dli7z16
dlnpp
mknpp
mkdsk
mv notepad-plus-plus bin && mv notepad-plus-plus.desktop usr/share/applications && mv notepad-plus-plus.png usr/share/pixmaps && cp -R -p {bin,usr,wine-runtime,wine-platform,winedata} $SNAPCRAFT_PART_INSTALL